package com.wzk.travelspringboot.controller;
import com.wzk.travelspringboot.dto.BlogLikedDTO;
import com.wzk.travelspringboot.dto.Result;
import com.wzk.travelspringboot.entity.Blog;
import com.wzk.travelspringboot.service.BlogService;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: WangZiKang
 * @Date: 2025/1/15 21:49
 */

@Slf4j
@RestController
@RequestMapping("/blog")
@RequiredArgsConstructor
public class BlogController {

    private final BlogService blogService;

    @PostMapping("/publish")
    @Validated
    public Result publish(
            @RequestParam("userId") @NotNull(message = "用户id不能为空") String userId,
            @RequestParam("content") @Length(min = 2,max = 5000,message = "内容长度不在合法范围内") String content,
            @RequestParam("images") MultipartFile[] images){
        return blogService.publishBlog(Long.valueOf(userId),content,images);
    }

    @PostMapping("/all")
    public Result getAllBlog(){
        return blogService.queryAllBlog();
    }

    @PostMapping("/liked")
    public Result liked(@RequestBody BlogLikedDTO dto){
        return blogService.isLikedBlog(dto.getBlogId(),dto.getUserId());
    }

    @GetMapping("/user/{id}")
    public Result blogByUserId(@Validated @PathVariable("id") @NotNull(message = "用户id不能为空") Long id){
        return blogService.queryBlogByUserId(id);
    }


    @DeleteMapping("/user/del")
    public Result delBlogById(@Validated @RequestParam("id") @NotNull(message = "动态id不能为空") Long id){
        return blogService.delByBlogId(id);
    }

}
